package com.bj.disc.ui.login.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import androidx.fragment.app.FragmentActivity
import com.ai3d.rfid.R
import com.ai3d.rfid.common.libs.InitLanguage
import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.databinding.ItemLanguageBinding
import com.ai3d.rfid.databinding.PopupMenuLanguageBinding
import com.bj.disc.util.DiscConstants
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @ProjectName : Trias
 * @Author : weijinsong
 * @Time : 2023/6/16 10:50
 * @Description : 文件描述
 */
class LanguagePop :
    PopupWindow {
    companion object {
        private const val TAG = "MenuShortcutPop"
        var SHOW_TYPE_MAIN_LANGUAGE = 1
        var SHOW_TYPE_ASSIST_LANGUAGE = 2
    }

    constructor(
        activity: FragmentActivity,
        showType: Int,
        inputAccount: String?,
        inputPassWord: String?,
        callBack: ((resId: Int) -> Unit)?
    ) : super(activity) {
        binding = PopupMenuLanguageBinding.inflate(LayoutInflater.from(activity))
        contentView = binding.root
        isOutsideTouchable = true
        isFocusable = true
        this.activity = activity
        this.showType = showType
        this.inputAccount = inputAccount
        this.inputPassWord = inputPassWord
        this.callBack = callBack
        initView()
        initData()
        initEvent()
//        setBackgroundDrawable(activity.getDrawable(R.drawable.layer_menu_language))
        setBackgroundDrawable(activity.getDrawable(R.drawable.bg_login_register_edit_shape))
    }

    lateinit var activity: FragmentActivity
    lateinit var binding: PopupMenuLanguageBinding
    lateinit var languageMainAdapter: LanguageMainAdapter
    lateinit var languageAssistAdapter: LanguageAssistAdapter
    var languages = mutableListOf<String>()
    var showType: Int = SHOW_TYPE_MAIN_LANGUAGE
    var inputAccount: String? = null
    var inputPassWord: String? = null
    var callBack: ((resId: Int) -> Unit)? = null
    fun show(view: View) {
        if (activity.window.decorView.windowToken != null) {
            showAsDropDown(view, 0, 0, Gravity.CENTER)
        }
    }

    fun initView() {
        var languageArray = activity.resources.getStringArray(R.array.language_array)
        languageArray.forEach {
            languages.add(it)
        }

        when (showType) {
            SHOW_TYPE_MAIN_LANGUAGE -> {
                binding.rcvMainLanguage.visibility = View.VISIBLE
                binding.rcvAssistLanguage.visibility = View.GONE
                languageMainAdapter = LanguageMainAdapter()
                binding.rcvMainLanguage.adapter = languageMainAdapter
            }
            SHOW_TYPE_ASSIST_LANGUAGE -> {
                binding.rcvMainLanguage.visibility = View.GONE
                binding.rcvAssistLanguage.visibility = View.VISIBLE
                languageAssistAdapter = LanguageAssistAdapter()
                binding.rcvAssistLanguage.adapter = languageAssistAdapter
            }
        }
    }

    fun initData() {
    }

    fun initEvent() {

    }

    inner class LanguageMainAdapter :
        BaseQuickAdapter<String, BaseDataBindingHolder<ItemLanguageBinding>>(
            R.layout.item_language, languages
        ) {
        override fun convert(holder: BaseDataBindingHolder<ItemLanguageBinding>, item: String) {
            holder.dataBinding?.tvLanguage?.text = item
            holder.dataBinding?.tvLanguage?.setOnClickListener {
                when (holder.layoutPosition) {
                    0 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
                        )
                        callBack?.invoke(R.string.english)
                    }
                    1 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN
                        )
                        callBack?.invoke(R.string.chinese)
                    }
                    2 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_HOKKIEN, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_HOKKIEN
                        )
                        callBack?.invoke(R.string.hokkien)
                    }
                    3 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_CANTONESE, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_CANTONESE
                        )
                        callBack?.invoke(R.string.cantonese)
                    }
                    4 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_TAMIL
                        )
                        callBack?.invoke(R.string.tamil)
                    }
                    5 -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US, inputAccount, inputPassWord)
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_MALAY
                        )
                        callBack?.invoke(R.string.malay_lan)
                    }
                    else -> {
                        InitLanguage.setAppLanguage(DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN, inputAccount, inputPassWord)
                        callBack?.invoke(R.string.chinese)
                    }
                }
                dismiss()
            }
        }
    }

    inner class LanguageAssistAdapter :
        BaseQuickAdapter<String, BaseDataBindingHolder<ItemLanguageBinding>>(
            R.layout.item_language, languages
        ) {
        override fun convert(holder: BaseDataBindingHolder<ItemLanguageBinding>, item: String) {
            holder.dataBinding?.tvLanguage?.text = item
            holder.dataBinding?.tvLanguage?.setOnClickListener {
                when (holder.layoutPosition) {
                    0 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
                        )
                        callBack?.invoke(R.string.english)
                    }
                    1 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN
                        )
                        callBack?.invoke(R.string.chinese)
                    }
                    2 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_HOKKIEN
                        )
                        callBack?.invoke(R.string.hokkien)
                    }
                    3 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_CANTONESE
                        )
                        callBack?.invoke(R.string.cantonese)
                    }
                    4 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_TAMIL
                        )
                        callBack?.invoke(R.string.tamil)
                    }
                    5 -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_MALAY
                        )
                        callBack?.invoke(R.string.malay_lan)
                    }
                    else -> {
                        MMKVProxy.setString(
                            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
                            DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN
                        )
                        callBack?.invoke(R.string.chinese)
                    }
                }
                dismiss()
            }
        }
    }
}